import torch
from torch.utils.data import Dataset
import os
from PIL import Image
import numpy as np
import config

class GTZANDataset(Dataset):
    def __init__(self, root, label, transform=None):
        self.root = root
        self.images = self.get_file_paths(root)
        self.transform = transform
        self.label = label

    def __len__(self):
        return len(self.images)

    def __getitem__(self, index):
        image = self.images[index]
        genre = image[:image.index('0')]

        path = os.path.join(self.root, genre, image)
        image = np.array(Image.open(path).convert("RGB"))

        if self.transform:
            augmentations = self.transform(image=image)
            image = augmentations["image"]

        return image, self.label[genre]

    @staticmethod
    def get_file_paths(root):
        paths = []
        for _, _, files in os.walk(root):
            paths.extend(files)
        return paths
